/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MetricsRetriever.RetrieverComponents;
import DataStructures.*;
import com.google.gdata.client.projecthosting.*;
import com.google.gdata.data. projecthosting.*;
import java.net.URL;
import java.util.Date;

/**
 *
 * @author Manuel
 */
public class GCBugManager extends BugManager{

    private String projectName;

    public GCBugManager(String p) {
        projectName = p;
    }

    public BugData getAllBugData() {
        BugData bugs = new BugData();
        try {
            // Request the feed
            ProjectHostingService myService = new ProjectHostingService(projectName);
            URL feedUrl = new URL("https://code.google.com/feeds/issues/p/" + projectName + "/issues/full");
            IssuesQuery myQuery = new IssuesQuery(feedUrl);
            //retrieve all defects, by setting label
            myQuery.setCan("all");
            myQuery.setMaxResults(10000);
            myQuery.setLabel("Type-Defect");
            IssuesFeed resultFeed = myService.getFeed(myQuery, IssuesFeed.class);
            // Store the results
            Bug currentBug;
            IssuesEntry entry;
            for (int i = 0; i < resultFeed.getEntries().size(); i++) {
                 entry = resultFeed.getEntries().get(i);
                 currentBug = extractBugInformation(entry);
                 if(this.checkIfSufficient(currentBug))
                     bugs.addBug(currentBug);
            }
        } catch (Exception ex) {
            System.out.println("Could not retrieve bug data for "+ this.projectName);
        }
        return bugs;
    }


    private Bug extractBugInformation(IssuesEntry entry) {

        Bug bug = new Bug();

        if(entry.getPublished() != null)
            bug.setCreationDate(new Date(entry.getPublished().getValue()));
        //extract owner id

        if(entry.hasOwner()){
            String ownerID = entry.getOwner().getUri().getValue();
            bug.setOwner(ownerID.substring(3,ownerID.lastIndexOf("/")));
        }

        if(entry.hasClosedDate())
            bug.setSolvedDate(new Date(entry.getClosedDate().getValue().getValue()));

        //extract author id
        if(entry.getAuthors() != null && entry.getAuthors().get(0) != null){
            String authorID = entry.getAuthors().get(0).getUri();
            bug.setAuthor(authorID.substring(3,authorID.lastIndexOf("/")));
        }

        if(entry.getTitle() != null)
            bug.setTitle(entry.getTitle().getPlainText());

        if(entry.getTextContent().getContent() != null)
            bug.setDescription(entry.getTextContent().getContent().getPlainText());

        if(entry.hasState())
            bug.setStatus(entry.getState().getValue().name().toLowerCase());

        if(entry.hasIssueId())
            bug.setIssueID(entry.getIssueId().getValue().toString());
        
        if(entry.hasStatus())
            bug.setResolution(entry.getStatus().getValue().toString().toLowerCase());

        //System.out.println("\nTitle: "+bug.getTitle()+"\nDescr  "+bug.getDescription()+"\nOwner  "+bug.getOwner()+"\nStatus  "+bug.getStatus()+"\nCreated "+bug.getCreationDate()+"\nClosed "+bug.getSolvedDate()+"\nAuthor "+bug.getAuthor());


        return bug;
    }




}
